import sys

from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QApplication, QDialog
from PyQt5.QtCore import Qt


class LoginIn(QDialog):
    # 定义一个信号，验证通过后发送给主窗口
    data_login = QtCore.pyqtSignal(str)

    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()

        self.setWindowTitle("login")

        # 用户名输入框
        user_box = QHBoxLayout()
        user_label = QLabel("用户名:")
        user_label.setFixedWidth(80)
        user_label.setAlignment(Qt.AlignRight)
        self.user_text = QLineEdit()
        user_box.addWidget(user_label)
        user_box.addWidget(self.user_text)
        user_box.addStretch()

        # 密码框
        pass_box = QHBoxLayout()
        pass_label = QLabel("密码:")
        pass_label.setFixedWidth(80)
        pass_label.setAlignment(Qt.AlignRight)
        self.pass_text = QLineEdit()
        pass_box.addWidget(pass_label)
        pass_box.addWidget(self.pass_text)
        pass_box.addStretch()

        # 登录按钮
        button_box = QHBoxLayout()
        login_out_button = QPushButton("退出")
        login_in_button = QPushButton("登录")
        login_in_button.clicked.connect(self.login)
        button_box.addStretch()
        button_box.addWidget(login_out_button)
        button_box.addWidget(login_in_button)

        layout.addLayout(user_box)
        layout.addLayout(pass_box)
        layout.addLayout(button_box)
        self.setLayout(layout)
        self.setFixedSize(320, 240)

    def login(self):
        if self.user_text.text() == "admin" and self.pass_text.text() == "admin":
            print("验证通过")
            self.data_login.emit(self.user_text.text())
            self.accept()   # 关闭模态对话框，允许主窗口继续运行
        else:
            print("验证未通过")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = LoginIn()
    main_window.show()
    sys.exit(app.exec_())
